package com.usian.quartz;

import com.usian.mq.MQSender;
import com.usian.pojo.LocalMessage;
import com.usian.pojo.TbOrder;
import com.usian.redis.RedisClient;
import com.usian.service.OrderService;
import com.usian.service.localMessageService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class OrderQuartz implements Job {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private localMessageService localMessageService;

    @Autowired
    private MQSender mqSender;

    /**
     * 关闭超时订单
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //1、未支付
        //2、超时

        //1、查询超时订单
        List<TbOrder> tbOrderList = orderService.selectOverTimeTbOrder();

        //2、关闭超时订单
        for (int i = 0; i < tbOrderList.size(); i++) {
            TbOrder tbOrder =  tbOrderList.get(i);
            orderService.updateOverTimeTbOrder(tbOrder);

            //3、把超时订单中的商品库存数量加回去
            orderService.updateTbItemByOrderId(tbOrder.getOrderId());
        }
        //-----------------------------扫描未发送消息-----------------------------------
        System.out.println("执行扫描本地消息表的任务...." + new Date());
        List<LocalMessage> localMessageList = localMessageService.selectlocalMessageByStatus();
        for (int i = 0; i < localMessageList.size(); i++) {
            LocalMessage localMessage =  localMessageList.get(i);
            mqSender.sendMsg(localMessage);
        }

    }
}
